# -*- coding:UTF-8 -*-

import os
from openpyxl import load_workbook
from os import path

from message import *

params = [{"column":1, "name":"书籍id", "key":"outerId", "parent":"", "isNull":True},
          {"column":2, "name":"书籍简介", "key":"desc", "parent":"productInfo", "isNull":True},
          {"column":3, "name":"售价", "key":"price", "parent":"productInfo", "isNull":True},
          {"column":4, "name":"内容地址", "key":"bookContentUrl", "parent":"productInfo", "isNull":True},
          {"column":5, "name":"ISBN编号", "key":"isbn", "parent":"productInfo", "isNull":False},
          {"column":6, "name":"封面图", "key":"coverUrl", "parent":"productInfo", "isNull":False},
          {"column":7, "name":"作者名", "key":"authorName", "parent":"productInfo", "isNull":True},
          {"column":8, "name":"作者地区", "key":"authorArea", "parent":"productInfo", "isNull":True},
          {"column":9, "name":"译者", "key":"translator", "parent":"productInfo", "isNull":True},
          {"column":10,"name":"编者",  "key":"editor", "parent":"productInfo", "isNull":True},
          {"column":11, "name":"书籍名称", "key":"bookName", "parent":"productInfo", "isNull":False},
          {"column":12, "name":"商品类目id", "key":"categoryId", "parent":"productInfo", "isNull":False},
          {"column":13, "name":"是否套装", "key":"series", "parent":"productInfo", "isNull":True},
          {"column":14, "name":"出版社", "key":"publisher", "parent":"productInfo", "isNull":True},
          {"column":15, "name":"出版时间", "key":"pubDate", "parent":"productInfo", "isNull":True},
          {"column":16, "name":"开本", "key":"format", "parent":"productInfo", "isNull":True},
          {"column":17, "name":"商品图片", "key":"imageUrls", "parent":"productInfo", "isNull":True, "split":","},
          {"column":18, "name":"商品标题", "key":"title", "parent":"itemInfo", "isNull":False},
          {"column":19, "name":"返回消费者天猫积分", "key":"auctionPoint", "parent":"itemInfo", "isNull":False},
          {"column":20, "name":"商品价格", "key":"itemPrice", "parent":"itemInfo", "isNull":False},
          {"column":21, "name":"所在市", "key":"city", "parent":"itemInfo", "isNull":False},
          {"column":22, "name":"所在省", "key":"prov", "parent":"itemInfo", "isNull":False},
          {"column":23, "name":"作者简介", "key":"authorDesc", "parent":"itemInfo", "isNull":False},
          {"column":24, "name":"编辑推荐语", "key":"recommend", "parent":"itemInfo", "isNull":False}]

'''
Excel文件读写列
# @author bricker
'''
class rwexcel:

    def __init__(self, file):
        self.filePath = file
    def read(self):
        m = Message()
        m.code = ErrorCode.c.SUCCESS
        if not self.filePath or not path.exists(self.filePath) or path.isdir(self.filePath):
            m.code = ErrorCode.c.FILERROR
            m.msg = '文件不存在/文件错误！！！'
        elif self.isXlsx():
            m = self.readxlsx()
        else:
            m.code = ErrorCode.c.FILERROR
            m.msg = '文件类型错误！！！'
        return m
    def readxlsx(self):
        m = Message()
        m.code = ErrorCode.c.SUCCESS
        data = []
        wb = load_workbook(self.filePath)
        sheet = wb.get_sheet_by_name(wb.sheetnames[0])
        for r in range(2, sheet.max_row + 1):# 循环行
            book = {}
            for param in params:
                c = param.get('column')
                param_parent = param.get('parent')
                param_key = param.get('key')
                param_name = param.get('name')
                value = sheet.cell(row = r, column = c).value
                isNull = param.get('isNull')
                split = param.get('split')
                # 判断是否为空
                if not value:
                    if isNull:# 字段可以为空的时候
                        continue
                    else:
                        m.code = ErrorCode.c.ILLEGALCONTENT
                        m.msg = '字段“%s”不能为空;但是%d行%d列此时为空'%(param_name, r, c)
                        return m
                if split:
                    value = value.split(split)

                if param_parent == '':
                    book[param_key] = value
                else:
                    parent = book.get(param_parent)
                    if not parent:
                        book[param_parent] = {}
                    book[param_parent][param_key] = value
            data.append(book)
        m.obj = data
        return m

    def isXlsx(self):
        suffix = path.splitext(self.filePath)[1][1:]
        if suffix == 'xlsx':return True
        else: return False